package com.yuanfuxing.boxuegu.fragment;

import android.app.Fragment;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanfuxing.boxuegu.R;
import com.yuanfuxing.boxuegu.activity.LoginActivity;

import static com.yuanfuxing.boxuegu.R.id.et_user_name;

/**
 * Created by Administrator on 2018/4/2.
 */

public class MyFragment extends Fragment {
    ImageView imageView;
    TextView textView;
    String userName;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        imageView = view.findViewById(R.id.iv_head_icon);
        textView = view.findViewById(R.id.iv_name);
        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Log.i("MyFragment","login");
                SharedPreferences sp = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
                boolean islogin = sp.getBoolean("islogin",false);
                String userName = sp.getString("username","点击登录");
                textView.setText(userName);
                if(islogin){
                    Toast.makeText(getActivity(),"已登录",Toast.LENGTH_LONG).show();
                    return;
                }
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        SharedPreferences sp = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
        userName = sp.getString("userName","点击登录");
        textView.setText(userName);
        super.onResume();
    }
}